package com.are;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.PowerManager;

public class ServiceGPS extends Service implements LocationListener {
	private static final long UPDATE_INTERVAL = 1000 * 60 * 3;
	private static final int TEN_METERS = 10;

	private final IBinder mBinder = new MyBinder();
	private LocationManager myLocationManager;
	private Location myLocation;
	private Timer timer = new Timer();
	private PowerManager.WakeLock wl;

	
	
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return mBinder;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		myLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

	}

	@Override
	public void onStart(final Intent intent, final int startId) {
		super.onStart(intent, startId);

		PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
		 PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
		 wl.acquire();
		 
		myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
				UPDATE_INTERVAL, TEN_METERS, this);

		timer.scheduleAtFixedRate(new TimerTask() {

			
			@Override
			public void run() {
				if (myLocationManager == null) {
					myLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
				}
				if (myLocation != null) {
					EnviarUbicacion();
				}
			}

		}, 0, UPDATE_INTERVAL);

	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (myLocationManager != null) {
			myLocationManager.removeUpdates(this);
		}
		wl.release();
	}

	public class MyBinder extends Binder {
		ServiceGPS getService() {
			return ServiceGPS.this;
		}
	}

	public void onLocationChanged(Location location) {
		if (location != null) {
			myLocation = location;
		}
	}

	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		myLocation = null;
	}

	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		switch (status) {
		case LocationProvider.AVAILABLE:

			break;
		case LocationProvider.OUT_OF_SERVICE:
			myLocation = null;
			break;
		case LocationProvider.TEMPORARILY_UNAVAILABLE:
			myLocation = null;
			break;
		}

	}

	private void EnviarUbicacion() {
		new Thread(new Runnable() {
			public void run() {
				String imei = "35885002174109101";
				String url;
				try {
					url = "http://siscos.fcr.com.co:8080/AreApp/SrvGps?imei="
							+ URLEncoder.encode(imei, "UTF-8")
							+ "&latitud="
							+ URLEncoder.encode(
									String.valueOf(myLocation.getLatitude()),
									"UTF-8")
							+ "&longitud="
							+ URLEncoder.encode(
									String.valueOf(myLocation.getLongitude()),
									"UTF-8");
					SendLocation conn = new SendLocation(url);
					conn.Send();
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}).start();

	}

}
